﻿using System;
using System.Collections.Generic;
using System.Web;
using System.IO;
using System.Security.AccessControl;

namespace ImbedLibrary
{
    public class FolderTools
    {

        /// <summary>
        /// 创建文件夹
        /// </summary>
        /// <param name="path">新建文件夹路径</param>
        /// <param name="userName">用户名</param>
        /// <returns>信息</returns>
        public static string CreateFolder(string path, string userName)
        {
            string message = "ok";
            try
            {
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);//创建文件夹
                    SetFolderACL(path, userName, FileSystemRights.Write | FileSystemRights.Read, AccessControlType.Allow);
                }
                else
                    message = "文件夹已存在";
            }
            catch (Exception)
            {
                throw;
            }
            return message;
        }

        /// <summary>
        /// 设置权限
        /// </summary>
        /// <param name="folderPath">文件夹路径</param>
        /// <param name="UserName">用户名</param>
        /// <param name="Rights">权限</param>
        /// <param name="AllowOrDeny"></param>
        /// <returns></returns>
        public static bool SetFolderACL(String folderPath, String UserName, FileSystemRights Rights, AccessControlType AllowOrDeny)
        {
            InheritanceFlags inherits = InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit;
            return SetFolderACL(folderPath, UserName, Rights, AllowOrDeny, inherits, PropagationFlags.None, AccessControlModification.Add);
        }

        public static bool SetFolderACL(String folderPath, String UserName, FileSystemRights Rights, AccessControlType AllowOrDeny, InheritanceFlags Inherits, PropagationFlags PropagateToChildren, AccessControlModification AddResetOrRemove)
        {
            bool ret;
            DirectoryInfo folder = new DirectoryInfo(folderPath);
            DirectorySecurity dSecurity = folder.GetAccessControl(AccessControlSections.All);
            FileSystemAccessRule accRule = new FileSystemAccessRule(UserName, Rights | FileSystemRights.Write, Inherits, PropagateToChildren, AllowOrDeny);
            dSecurity.ModifyAccessRule(AddResetOrRemove, accRule, out ret);
            folder.SetAccessControl(dSecurity);
            return ret;
        }


        /// <summary>
        /// 拷贝文件夹
        /// </summary>
        /// <param name="srcdir">被复制文件夹</param>
        /// <param name="desdir">新地址</param>
        static public void CopyDirectory(string srcdir, string desdir)
        {
            string folderName = srcdir.Substring(srcdir.LastIndexOf("\\") + 1);

            string desfolderdir = desdir + "\\";

            if (desdir.LastIndexOf("\\") == (desdir.Length - 1))
            {
                desfolderdir = desdir + folderName;
            }
            string[] filenames = Directory.GetFileSystemEntries(srcdir);

            foreach (string file in filenames)// 遍历所有的文件和目录
            {
                if (Directory.Exists(file))// 先当作目录处理如果存在这个目录就递归Copy该目录下面的文件
                {

                    string currentdir = desfolderdir + "\\" + file.Substring(file.LastIndexOf("\\") + 1);
                    if (!Directory.Exists(currentdir))
                    {
                        Directory.CreateDirectory(currentdir);
                    }

                    CopyDirectory(file, desfolderdir);
                }

                else // 否则直接copy文件
                {
                    string srcfileName = file.Substring(file.LastIndexOf("\\") + 1);

                    srcfileName = desfolderdir + "\\" + srcfileName;


                    if (!Directory.Exists(desfolderdir))
                    {
                        Directory.CreateDirectory(desfolderdir);
                    }


                    File.Copy(file, srcfileName,true);
                }
            }//foreach
        }//function end
    }
}
